package com.primary.controller;

import com.primary.common.Result;
import com.primary.controller.request.DonationsitesRequest;
import com.primary.entity.Donationsites;
import com.primary.service.DonationsitesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sites")
public class DonationsitesController {

    @Autowired
    DonationsitesService donationsitesService;

    @RequestMapping(value = "/page",method = {RequestMethod.GET})
    public Result page(DonationsitesRequest donationsitesRequest){
        return Result.success(donationsitesService.queryByPage(donationsitesRequest));
    }

    @RequestMapping(value = "/addOne", method = {RequestMethod.POST})
    public Result insertOneUser(@RequestBody Donationsites donationsites) {
        if (donationsites.getSitename() != null) {
            return donationsitesService.insertOne(donationsites) > 0 ?
                    Result.success() : Result.error("-114","插入错误");
        } else {
            return Result.error("请输入所有信息");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateOne",method = {RequestMethod.PUT})
    public Result updateOne(@RequestBody Donationsites donationsites){
        if (null != donationsites.getSitename()){
            return donationsitesService.updateOneById(donationsites) > 0 ?
                    Result.success() : Result.error("更新失败!");
        } else {
            return Result.error("信息不能为空！");
        }
    }

    @RequestMapping(value = "/deleteOne/{id}",method = {RequestMethod.DELETE})
    public Result deleteById(@PathVariable Integer id){
        return null == id ? Result.error("ID不能为空") :
                (donationsitesService.deleteById(id) ? Result.success():Result.error("删除失败"));
    }

    @RequestMapping(value = "/allsites", method = {RequestMethod.GET})
    public Result allBloodbanks() {
        List<String> list = donationsitesService.querySitesForSelecter();
        return Result.success(list);
    }

    @RequestMapping(value = "/sitesandcars", method = {RequestMethod.GET})
    public Result allSitesandcars() {
        List<String> list = donationsitesService.querySitesAndCarsForSelecter();
        return Result.success(list);
    }


}
